package com.metroinfo.test.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

import com.metroinfo.parser.GeneralSettings;

public class SimpleClientThread implements Runnable {

	Socket smtpSocket = null;
	DataOutputStream os = null;
	DataInputStream is = null;
	short broj = 0;
	public SimpleClientThread(short br)
	{
		broj = br;

		try {
			
			smtpSocket = new Socket("localhost", 8000);
			os = new DataOutputStream(smtpSocket.getOutputStream());
			is = new DataInputStream(smtpSocket.getInputStream());
			// bis = new BufferedReade(smtpSocket.getInputStream());
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: hostname");
		} catch (IOException e) {
			System.err
					.println("Couldn't get I/O for the connection to: hostname - " + e.getMessage());
		}

	}
	
	@Override
	public void run() {

		if (smtpSocket != null && os != null && is != null) {
			try {
				ByteBuffer bb = ByteBuffer.allocate(10);
				bb.put((byte)GeneralSettings.CT_SEND_DATA);
				bb.putShort((short)broj);
				bb.putShort((short)broj);
				bb.put((byte)1);
				
				bb.putShort((short)323);
				bb.put((byte)23);
				
				bb.put((byte)0);
				os.write( bb.array());
				os.flush();
				String responseLine;
				responseLine = is.readLine();
				
				os.close();
				is.close();
				smtpSocket.close();
				//System.out.println("Odgovor je = "+ responseLine);
			} catch (UnknownHostException e) {
				System.err.println("Trying to connect to unknown host: " + e);
			} catch (IOException e) {
				System.err.println("IOException:  " + e);
			}
		}
	
	}

}
